<!DOCTYPE html>
<html>
<?php $this->load->view('admin/public/header.html');?>
<style>
	. space{padding:10px;}

</style>
<body class="childrenBody">
<form class="layui-form layui-row layui-col-space20" lay-filter="expert_form">
	<div class="layui-col-md6 layui-col-xs12 ">
		<div class="layui-form-item magt3">
					<label class="layui-form-label">用户名</label>
					<input type="hidden" name="adminId">
					<div class="layui-input-block">
						<input type="text" class="layui-input name" name="adminName" placeholder="请输入用户名" lay-verify="require"  >
					</div>
				</div>
				
				<div class="layui-form-item magt3">
					<label class="layui-form-label">密码</label>
					<div class="layui-input-block">
						<input type="password" class="layui-input name" name="password" placeholder="请输入密码" lay-verify="require"  >
					</div>
				</div>
				
				<div class="layui-form-item magt3">
					<label class="layui-form-label">真实姓名</label>
					<div class="layui-input-block">
						<input type="text" class="layui-input name" name="realName" placeholder="请输入真实姓名" lay-verify="require"  >
					</div>
				</div>
				
				<div class="layui-form-item magt3">
					<label class="layui-form-label">性别</label>
					<div class="layui-input-block">
						<input type="radio" name="sex" value="1" title="男" checked="checked">
      					<input type="radio" name="sex" value="2" title="女" >
					</div>
				</div>

				<div class="layui-form-item magt3">
					<label class="layui-form-label">管理员角色</label>
					<div class="layui-input-block">
						<select name="roleId" lay-verify="require" lay-search>
						  <option value="">请选择管理员角色</option>
						  <?php foreach($role as $key=>$val):?>
						  	<option value="<?php echo $val['roleId']?>"><?php echo $val['roleName']?> </option>
						  <?php endforeach;?>
						</select>
					</div>
				</div>
				<div class="layui-right">
					<a class="layui-btn layui-btn-sm" lay-filter="addNews" lay-submit><i class="layui-icon">&#xe609;</i>保存</a>
			</div>
	</div>
</form>
<script type="text/javascript">

layui.use(['form','layer','layedit','laydate','upload'],function(){
	
	
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;


   

    //上传缩略图
    upload.render({
        elem: '.thumbBox',
        url: '../../json/userface.json',
        method : "get",  //此处是为了演示之用，实际使用中请将此删除，默认用post方式提交
        done: function(res, index, upload){
            var num = parseInt(4*Math.random());  //生成0-4的随机数，随机显示一个头像信息
            $('.thumbImg').attr('src',res.data[num].src);
            $('.thumbBox').css("background","#fff");
        }
    });

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    
    

	form.verify({
        
        require : function(val,dom){
        	var name=($(dom).attr('name'));
        	var tips='必填项';
        	switch (name) {
			case 'adminName':
				tips='用户名'
				break;
			case 'realName':
				tips='真实姓名'
				break;
			case 'password':
				tips='密码'
				break;
			case 'roleId':
				tips='管理员角色'
				break;
			case 'sex':
				tips='性别'
				break;
			
			default:
				break;
			}
        	console.log(val+name);
            if(val == ''){
                return tips+"不能为空";
            }
        },
        
        
//        content : function(val){
//            if(val == ''){
//                return "内容不能为空";
//            }
//        }
    })
    
    form.on("submit(addNews)",function(data){
    	
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        
        $.ajax({
        	type:'post',
        	data:data.field,
        	url:'action',
        	dataType: "json", 
        	success:function(res){
        		//console.log(res.code);return;
        		if(res.code==200){
        			top.layer.close(index);
                    top.layer.msg("添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
        		}else{
        			top.layer.close(index);
                    top.layer.msg("添加失败！");
        		}
        	},
        	fail:function(){
        		top.layer.close(index);
                top.layer.msg("添加失败！");
        	}
        })        
        
    })

    
    
    
})
</script>
</body>
</html>